package com.tnr.scgcxx.service.safty.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tnr.scgcxx.dao.safty.HomeDao;
import com.tnr.scgcxx.dto.MenuDto;
import com.tnr.scgcxx.model.Module;
import com.tnr.scgcxx.service.safty.HomeService;

@Service
public class HomeServiceImpl implements HomeService{
	
	@Autowired
	private HomeDao homeDao;
	
	@Override
	public List<MenuDto> getMenus() {
		List<Module> moduleList = homeDao.findMenus();
		
		List<MenuDto> mainMenuList = new ArrayList<MenuDto>();
		
		MenuDto currMainMenu = null;
		for(Module m:moduleList){
			if(m.getM_pid()==null){
				currMainMenu = new MenuDto();
				currMainMenu.setMenuId(m.getM_id());
				currMainMenu.setMenuName(m.getM_name());
				currMainMenu.setSubMenuList(new ArrayList<MenuDto>());	
				mainMenuList.add(currMainMenu);
			}else{
				MenuDto subMenu=new MenuDto();
				subMenu.setMenuId(m.getM_id());
				subMenu.setMenuName(m.getM_name());
				subMenu.setMenuUrl(m.getM_url());
				
				currMainMenu.getSubMenuList().add(subMenu);
			}
		}
		return mainMenuList;
	}

}
